﻿using Rainbow.core;
using Rainbow.model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Rainbow.view
{
    /// <summary>
    /// ConnectManagerDlg.xaml 的交互逻辑
    /// </summary>
    public partial class ConnectManagerDlg : Window
    {
        private ConnectManagerDlgViewModel windowViewModel;

        private ConnectInfoCollection connectInfoCollection;

        public ConnectInfoViewModel resultInfo;

        public ConnectManagerDlg(ObservableCollection<ConnectInfoViewModel> infos)
        {
            InitializeComponent();

            windowViewModel = new ConnectManagerDlgViewModel(infos);
            this.DataContext = windowViewModel;

            this.btnAdd.Click += new RoutedEventHandler(Add_Click);
            this.btnEdit.Click += new RoutedEventHandler(Edit_Click);
            this.btnDelete.Click += new RoutedEventHandler(Delete_Click);

            this.lbInfos.MouseDoubleClick += new MouseButtonEventHandler(MouseDouble_Click);

            connectInfoCollection = TW_App.connectInfos;
        }

        private void MouseDouble_Click(object sender, MouseButtonEventArgs e)
        {
            if (this.lbInfos.SelectedItem == null) return;

            ConnectInfoViewModel info = lbInfos.SelectedItem as ConnectInfoViewModel;
            if (info == null) return;

            resultInfo = info;

            this.DialogResult = true;
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            AddConnectDlg dlg = new AddConnectDlg(AddConnectDlg.OPEnum.NEW,null);
            dlg.Owner = this;
            dlg.WindowStartupLocation = WindowStartupLocation.CenterOwner;

            bool? result = dlg.ShowDialog();
            if (result == true)
            {
                ConnectInfoViewModel connectInfoViewModel = dlg.windowViewModel.info;
                windowViewModel.infos.Add(connectInfoViewModel);

                ConnectInfo info = new ConnectInfo(connectInfoViewModel);
                connectInfoCollection.Add(info);
                connectInfoCollection.Save();
            }
        }

        private void Edit_Click(object sender, RoutedEventArgs e)
        {
            if (this.lbInfos.SelectedItem == null) return;

            ConnectInfoViewModel info = lbInfos.SelectedItem as ConnectInfoViewModel;

            AddConnectDlg dlg = new AddConnectDlg(AddConnectDlg.OPEnum.UPDATE, info);
            dlg.Owner = this;
            dlg.WindowStartupLocation= WindowStartupLocation.CenterOwner;

            bool? result = dlg.ShowDialog();
            if(result == true)
            {
                string name = info.name;
                ConnectInfoViewModel newObj = dlg.windowViewModel.info;
                ConnectInfoViewModel updObj = windowViewModel.infos.Where(x=>x.name == name).FirstOrDefault();

                if(updObj != null)
                {
                    updObj.Copy(newObj);
                }

                ConnectInfo connectInfo = new ConnectInfo(newObj);
                connectInfoCollection.Update(name, connectInfo);

                connectInfoCollection.Save();
            }
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("确认要删除吗？","删除确认",MessageBoxButton.YesNo,MessageBoxImage.Question);

            if (result == MessageBoxResult.No) return;

            if (this.lbInfos.SelectedItem == null) return;

            ConnectInfoViewModel info = lbInfos.SelectedItem as ConnectInfoViewModel;

            windowViewModel.infos.Remove(info);
            connectInfoCollection.Delete(info.name);
            var tmp = connectInfoCollection.Where(x=>x.name == info.name).FirstOrDefault();
            if(tmp != null) connectInfoCollection.Delete(tmp);
            connectInfoCollection.Save();
        }
    }

    public class ConnectManagerDlgViewModel : ViewModelBase
    {
        public ConnectManagerDlgViewModel(ObservableCollection<ConnectInfoViewModel> list)
        {
            infos = new ObservableCollection<ConnectInfoViewModel>();
            if(list != null)
            {
                foreach(var item in list)
                {
                    infos.Add(item.Clone());
                }
            }
        }

        private ObservableCollection<ConnectInfoViewModel> m_infos;
        public ObservableCollection<ConnectInfoViewModel> infos
        {
            get { return m_infos; }
            set
            {
                m_infos = value;
                NotifyPropertyChanged();
            }
        }
    }
}
